<?php
/**
 * 后台节点添加控制器。
 *
 * @author    Fairyin <fairyin@126.com>
 * @copyright © 2016 www.imcn.vip
 * @version   v1.0
 */

namespace App\Http\Controllers\Shadow;

use View;
use Config;
use Response;
use Redirect;
use App\Http\Model\Type;
use Illuminate\Http\Request;

class TypeController extends ShadowController
{
    public function onGet()
    {
        $this->results['token'] = csrf_token();
        $this->results['types'] = Type::all();
        
        return View::make('shadow/type_list', $this->results);
    }
    
    public function onPost(Request $request)
    {
        $s_title = $request->get('title', '');
        $s_type = $request->get('type', '');
        if ($s_type == 'add_type') {
            $type = new Type();
            $type->title = $s_title;
            $type->save();
        } elseif ($s_type == 'qiyong_type') {
            $id = $request->get('id', '');
            $o_type = Type::find($id);
            $o_type->status = Type::ENABLE;
            $o_type->save();
        } elseif ($s_type == 'jinyong_type') {
            $id = $request->get('id', '');
            $o_type = Type::find($id);
            $o_type->status = Type::DELETED;
            $o_type->save();
        } elseif ($s_type == 'edit_type') {
            $id = $request->get('id', '');
            $s_title = $request->get('title', '');
            $i_count = Type::where('title', '=', $s_title)
                ->where('vt_id', '!=', $id)
                ->count();
            if (!$i_count) {
                $o_type = Type::find($id);
                $o_type->title = $s_title;
                $o_type->save();
            }
        }
        
        return Redirect::to('/shadow/photo/type');
    }
}
